import axios from 'axios'
import Vue from 'vue'
import router from './router'

const http = axios.create({
    baseURL: 'http://localhost:3030/admin/api'
})

http.interceptors.request.use(config => {
    sessionStorage.token ?
        config.headers.Authorization = 'Bearer ' + sessionStorage.token : ''
    return config
}, error => {
    return Promise.reject(error)
})

http.interceptors.response.use(res => {
    
    return res
}, error => {
    // console.log(error)
    if(error.response.data.message){
        Vue.prototype.$message({
            type: 'error',
            message: error.response.data.message
        })
        if(error.response.status === 401){
            router.push('/Login')
        }
    }
    
    return Promise.reject(error)
})

export default http